#include "KevinLayoutWdt.h"
#include "./ui_KevinLayoutWdt.h"

#include <QDebug>
#include <QFile>
#include <map>

std::map<QWidget*, QWidget*> widgetMap;

KevinLayoutWdt::KevinLayoutWdt(QWidget* parent)
    : QWidget(parent)
    , ui(new Ui::KevinLayoutWdt) {
    ui->setupUi(this);
    // QPushButton* but1 = new QPushButton(this);
    // but1->setText("lamda表达式");
    //  将but1直接写在括号中时，为以值传递的方式将but1这个指针传递进匿名函数里
    //  = ，表示将表达式外所有变量以值传递的方式传进匿名函数里，传进来为常量
    //  &
    //  ，表示将表达式外所有变量以引用传递的方式进入匿名函数里，传进动态指针会引发出错，内存释放的问题。
    //  mutable关键字，将传进去的值，改为普通变量，不再是常量了

    /*
    connect(ui->pushButton0, &QPushButton::clicked, [ = ]() mutable { switchPage(ui->widget0); });
    connect(ui->pushButton1, &QPushButton::clicked, [ = ]() mutable { switchPage(ui->widget1); });
    connect(ui->pushButton2, &QPushButton::clicked, [ = ]() mutable { switchPage(ui->widget2); });
    */
    connect(ui->pushButton3, &QPushButton::clicked, [ = ]() mutable { switchPage(ui->widget3); });
}
KevinLayoutWdt::~KevinLayoutWdt() {
    delete ui;
}
void KevinLayoutWdt::switchPage(QWidget* parent) {
    QString msg = tr("切换[%1]的显示. Lamda").arg(parent->objectName());
    ui->textEditMessage->append(msg);
    parent->setVisible(!parent->isVisible());
}
void KevinLayoutWdt::study() {
    int count = ui->verticalLayoutCenter->count();
    qDebug() << " KevinLayoutWdt::study() cout = " << count;

    for (size_t i = 0; i < count; i++) {
        QLayoutItem* layout1 = ui->verticalLayoutCenter->itemAt(i);
        QWidget*     widget  = layout1->widget();
        qDebug() << i << widget->geometry() << widget->metaObject()->className() << "," << widget;
        // if (obj->metaObject()->className() ==
        // QStringLiteral("QPushButton")这样就可确定其类型为一个按钮。
        //  item->widget()->dumpObjectInfo();
    }

    qDebug() << "  pair:";
    for (size_t i = 0; i < count; i++) {
        QLayoutItem* layout1 = ui->verticalLayoutCenter->itemAt(i);
        QWidget*     widget  = layout1->widget();
        if (i % 2 == 1) {
            QWidget* button = ui->verticalLayoutCenter->itemAt(i - 1)->widget();
            //
            qDebug() << i << button->objectName() << "=>" << widget->objectName();
            widgetMap[ button ] = widget;
            // connect(button, &QPushButton::clicked, [ = ]() mutable { switchPage(widget); });
        }
        // item->widget()->dumpObjectInfo();
    }

    // map
    qDebug() << "  map:";
    int i = 0;
    for (std::map<QWidget*, QWidget*>::iterator iter = widgetMap.begin(); // begin
         iter != widgetMap.end();                                         // judge
         iter++, i++) {
        connect(iter->first, SIGNAL(clicked()), this, SLOT(slotFun()), Qt::UniqueConnection);
        qDebug() << i << iter->first->objectName() << "=>" << iter->second->objectName();
    }
}
void KevinLayoutWdt::slotFun() {
    qDebug() << "   KevinLayoutWdt::slotFun():";
    QWidget* act = qobject_cast<QWidget*>(sender()); //使用Qt的类型转换，将指针恢复为QAction类型

    int      visibleCount = 0;
    QWidget* findWidget   = nullptr;

    for (std::map<QWidget*, QWidget*>::iterator iter = widgetMap.begin(); // begin
         iter != widgetMap.end();                                         // judge
         iter++) {
        if (iter->second->isVisible()) visibleCount++;
        if (iter->first == act) findWidget = iter->second;
    }

    // treat
    if (nullptr == findWidget) {

        qDebug() << "  映射表没有这个指针记录：" << act;
    }
    else {
        qDebug() << "  映射表发现：" << act;
        if (findWidget->isVisible()) {
            visibleCount--;
        }
        if (0 == visibleCount) {
            qDebug() << "  必须要显示一个" << act;
            findWidget->setVisible(true);
        }
        else {
            switchPage(findWidget);
        }
    }
}
